#include "Player.h"

Player::Player()
{
	m_pInput = Input::getInstance();
	m_speed = 500;
	bulletIndex = 0;
}

void Player::initialize(LPCWSTR fileName)
{
	m_texture.initialize(fileName);
}

void Player::initialize(LPCWSTR fileName, V2DF position, float scale, float rotation)
{
	m_texture.initialize(fileName);
	m_pos = position;
	m_scale = scale;
	m_rotation = rotation;
}

void Player::update(float dT)
{
	// orientate facing to the mouse pointer
	V2DF mouse = m_pInput->getMousePos();
	V2DF difference = mouse.difference(m_pos);
	m_rotation = atan2f(difference.y,difference.x)*(180.0f/(float)V2D_PI) + 90.0f;

	// player actions
	const int ACT_NONE = 0, MOV_U = 1, MOV_D = 2, MOV_L = 3, MOV_R = 4;
	int action = ACT_NONE;

	// player's old position
	V2DF oldpos = m_pos;
	
	// keep player within bounds of window
	// if camera is not on player
	
	// get user input
	if(m_pInput->keyDown(DIK_W))
		m_pos.y -= m_speed*dT;
	if(m_pInput->keyDown(DIK_S))
		m_pos.y += m_speed*dT;
	if(m_pInput->keyDown(DIK_A))
		m_pos.x -= m_speed*dT;
	if(m_pInput->keyDown(DIK_D))
		m_pos.x += m_speed*dT;
	// off screen conditions
	bool offScreenLeft = (m_pos.x-m_texture.getWidth()*0.5) < 0;
	bool offScreenRight = (m_pos.x+m_texture.getWidth()*0.5) > SCREEN_WIDTH;
	bool offScreenTop = (m_pos.y-m_texture.getHeight()*0.5) < 0;
	bool offScreenBottom = (m_pos.y+m_texture.getHeight()*0.5) > SCREEN_HEIGHT;
	if( offScreenTop )
		m_pos.y += m_speed*dT;

	if( offScreenLeft )
		m_pos.x += m_speed*dT;

	if( offScreenBottom )
		m_pos.y -= m_speed*dT;

	if( offScreenRight )
		m_pos.x -= m_speed*dT;
}

void Player::render()
{
	m_texture.draw(m_pos, m_rotation, m_scale, true);
}

void Player::release()
{
}